/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import boundless.ejb.RequestBean;
import boundless.entities.*;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
/**
 *Used to access League editing operations
 * @author hendrikdekock
 */
@Named("League")
@ManagedBean
@SessionScoped
public class LeagueBean {

    boolean inviteOnly;
    
    @EJB
    RequestBean rb;
    
    @Inject
    League curLeague;
    
    List<Joinrequests> requests;
    
    String error;
    boolean canDraft;
    /**
     * Creates a new instance of LeagueBean
     */
    public LeagueBean() {
    }
    
    public void setCurLeague(League l) {
        curLeague = l;
    }
    
    public void setReqBean(RequestBean r) {
        rb = r;
    }
    
    
    public void viewJoinRequests() {
        requests = rb.getJoinRequestsForLeague(curLeague);
        System.out.println("NUMBER OF REQUESTS: " + requests.size());
    }
    
    public List<Joinrequests> getJoinRequests() {
        return requests;
    }
    
    /**
     * Accepts a join request, adding that user to the league
     * @param jr 
     */
    
    public void acceptJoinRequest(Joinrequests jr) {
        //rb.addUserToLeague(jr.getAccount(), jr.getLeague());
        Account a = rb.getAccountById(jr.getJoinrequestsPK().getUserid());
        League l = rb.getLeague(jr.getJoinrequestsPK().getLeagueid());
        Collection<League> coll = a.getLeagueCollection();
        coll.add(l);
        l.getAccountCollection().add(a);
        rb.removeJoinRequest(jr);
        rb.removeInvitation(jr);
    }
    
    public void declineJoinRequest(Joinrequests jr) {
        rb.removeJoinRequest(jr);
        rb.removeInvitation(jr);
    }
    
    
    /**
     * Invites the person with the given email address.  If an account with that email exists,
     * that account is invited, otherwise an email is sent to the address with an invitation to join
     * Boundless Fantasy
     * @param email : The email address of the user to invite
     */
    public void inviteByEmail(String email) {
        Account a;
        if ((a = rb.getAccountByEmail(email)) != null) {
            System.out.println("Inviting by account");
            inviteByAccount(a);
        }
        else {
            System.out.println("Adding email invitation");
            rb.inviteByEmail(new Invitebyemail(curLeague.getOwnerid().getId(), curLeague.getId(), email));
        }
    }
    

    /**
     * Invites a user with an account given its account entity.
     * @param u : The user to send an invitation to
     */
    public void inviteByAccount(Account u) {
        rb.inviteByAccount(new Invitebyid(u, curLeague));
        
    }
    
    /*Begins the Draft.
     * Can only be called if canDraft is true  Opens the draft facelet
     */
    public void beginDraft() {
        
    }
    
    /**
     * Changes the permissions of the league--whether it is private or public to the opposite of what it is
     */
    public void togglePermissions() {
        curLeague.setInviteonly((short) (curLeague.getInviteonly() ^ (short) 1));
        rb.synchronize();
    }


}
